package com.lhf.springcloud.service;

import com.lhf.springcloud.DAO.StudentBasicDao;
import com.lhf.springcloud.DAO.StudentEssentialInformationDao;
import com.lhf.springcloud.Entity.StudentBasicData;
import com.lhf.springcloud.Entity.StudentBasicInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: StorangeServiceImpl
 * @Description:
 * @Author: liuhefei
 * @Date: 2019/4/1
 * @blog: https://www.imooc.com/u/1323320/articles
 **/
@Service
public class StudentEssentialInformationImpl {


    @Autowired
    private StudentEssentialInformationDao studentEssentialInformationDao;
    @Autowired
    private StudentBasicDao studentDao;

    public String addCourse(List<StudentBasicData> course) {
        /*在学生基础数据表里面查询身份证是否存在*/
        for (int i=0;i<course.size();i++){
            List<StudentBasicInfo> byStudentIdCard = studentDao.findByIdCard(course.get(i).getIdCard());
            if (byStudentIdCard.size()==0){
                course.get(i).setInfoJudge("false1");
                //return "学生基础信息中没有身份证为'"+course.get(i).getIdCard()+"',姓名为'"+course.get(i).getStudentName()+"',请检查该学生的身份证信息是否准确再上传数据";
            }
            if (byStudentIdCard.size()>1){
                return "联系管理员，基础数据身份证号码重复";
            }
            if (byStudentIdCard.size()==1){
                if (!byStudentIdCard.get(0).getStudentName().equals(course.get(i).getStudentName())){
                    course.get(i).setInfoJudge("false2");
                    //return "身份证为'"+course.get(i).getIdCard()+"',姓名为'"+course.get(i).getStudentName()+"',请检查该学生的姓名是否准确再上传数据";
                }
            }
        }


        for (int i=0;i<course.size();i++){
                studentEssentialInformationDao.addCourse(course.get(i));
        }
        return "完成操作";
    }

    public List<StudentBasicData> findAllStudentEssentialInformation() {
        List<StudentBasicData> data = studentEssentialInformationDao.findAllStudentEssentialInformation();
        return data;
    }

    public void updateData(String dataValue,String id,String dataName) {
        studentEssentialInformationDao.updateData(dataValue,id,dataName);//修改的同时初始化infoJudge字段为null
        //拿到修改对象的数据
        StudentBasicData byStudentId = studentEssentialInformationDao.findByStudentId(id);
        //判断基础数据中是否存在
        List<StudentBasicInfo> byStudentIdCard = studentDao.findByIdCard(byStudentId.getIdCard());
        if (byStudentIdCard.size()==0){
            studentEssentialInformationDao.updateStudentStatus("false1",id);
            //return "学生基础信息中没有身份证为'"+course.get(i).getIdCard()+"',姓名为'"+course.get(i).getStudentName()+"',请检查该学生的身份证信息是否准确再上传数据";
        }
        else if (byStudentIdCard.size()==1){
            if (!byStudentIdCard.get(0).getStudentName().equals(byStudentId.getStudentName())){
                studentEssentialInformationDao.updateStudentStatus("false2",id);
                //return "身份证为'"+course.get(i).getIdCard()+"',姓名为'"+course.get(i).getStudentName()+"',请检查该学生的姓名是否准确再上传数据";
            }else {
                studentEssentialInformationDao.updateStudentStatus(null,id);
            }
        }

        studentEssentialInformationDao.updateData(dataValue,id,dataName);
    }

    public List<StudentBasicData> findByIdCard(String idCard) {
        List<StudentBasicData> byIdCard = studentEssentialInformationDao.findByIdCard(idCard);
        return  byIdCard;
    }
}
